<?php


namespace app\adminapi\lists\user;


use app\adminapi\lists\BaseAdminDataLists;
use app\common\model\user\Openvpn;
use app\common\lists\ListsSearchInterface;
use app\common\model\user\OpenvpnDept;

/**
 * openvpn控制器列表
 * Class OpenvpnLists
 * @package app\adminapi\listsuser
 */
class OpenvpnLists extends BaseAdminDataLists implements ListsSearchInterface
{


    /**
     * @notes 设置搜索条件
     * @return \string[][]
     * @author likeadmin
     * @date 2024/07/04 15:15
     */
    public function setSearch(): array
    {
        return [
            '=' => ['sn', 'common_name', 'user_name', 'vpn_ip', 'status'],
        ];
    }


    /**
     * @notes 获取openvpn控制器列表
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * @author likeadmin
     * @date 2024/07/04 15:15
     */
    public function lists(): array
    {
        $where = [];
        if ($this->adminId != 1) {
            $openvpnDept = OpenvpnDept::where('dept_id', $this->adminInfo['dept_id'])->column('vpn_id');
            $where[] = ['id', 'in', $openvpnDept];
        }

        $list = Openvpn::where($this->searchWhere)
            ->where($where)
//            ->whereNotNull('sysinfo')
            ->field(['id', 'sn', 'common_name', 'user_name', 'ext_ip', 'vpn_ip', 'status', 'rx_bytes', 'tx_bytes', 'sysinfo', 'update_time'])
            ->limit($this->limitOffset, $this->limitLength)
            ->order(['id' => 'desc'])
            ->select()
            ->toArray();
        //读取所属部门
        foreach($list as &$row){
            $row['dept_id'] = OpenvpnDept::where(['vpn_id' => $row['id']])->column('dept_id');
        }

        return $list;
    }


    /**
     * @notes 获取openvpn控制器数量
     * @return int
     * @author likeadmin
     * @date 2024/07/04 15:15
     */
    public function count(): int
    {
        $where = [];
        if ($this->adminId != 1) {
            $openvpnDept = OpenvpnDept::where('dept_id', $this->adminInfo['dept_id'])->column('vpn_id');
            $where[] = ['id', 'in', $openvpnDept];
        }
        return Openvpn::where($this->searchWhere)->where($where)->count();
    }

}